<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="登录账户设置">
            <!-- 放置表单 -->
            <el-form ref="formData" label-width="120px" style="margin-left: 120px; margin-top:30px" :rules="rules" :model="userInfo">
              <el-form-item label="姓名:" prop="username">
                <el-input v-model="userInfo.username" style="width:300px" />
              </el-form-item>
              <el-form-item label="密码:" prop="password2">
                <el-input v-model="userInfo.password2" style="width:300px" type="password" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="saveUserInfo">更新</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="个人详情">
            <component :is="UserInfoEmploy" />
          </el-tab-pane>
          <el-tab-pane label="岗位信息">
            <component :is="JobInfoEmploy" />
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>>

<script>
// 获取id值 this.$router.params.id 为什么是params 因为路由写的是:id如果是id?就是query.id
// 1. 获取用户基本信息
// 2. 信息回写页面
// 3. 表单校验规则写
// 4. 校验成功的话
// 5. 调用保存信息接口
// 5.1 保存信息调用接口 接口参数注意 是解构赋值{...this.userInfo, password: this.userInfo.password2}
import { getUserDetailByIdAPI, saveUserInfoAPI } from '@/api'
import { mapActions } from 'vuex'
import UserInfo from './component/user-info'
import JobInfo from './component/job-info'

export default {
  data() {
    return {
      UserInfoEmploy: UserInfo,
      JobInfoEmploy: JobInfo,
      userId: this.$route.params.id,
      userInfo: {
        username: '',
        password2: ''
      }, // 用户的基本信息
      rules: {
        username: [{ required: true, message: '姓名不能为空', trigger: 'blur' }],
        password2: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 6, max: 9, message: '密码长度6-9位', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getUserInfo()
  },
  methods: {
    ...mapActions(['user/getUserInfo']),
    async getUserInfo() {
      this.userInfo = await getUserDetailByIdAPI(this.userId)
    },
    // 更新信息
    async saveUserInfo() {
      await this.$refs.formData.validate()
      // 上面 userInfo 被完全赋值为后台传来的数据
      // 所以这里解构出来 password：字段会被后面的password字段替换掉
      await saveUserInfoAPI({ ...this.userInfo, password: this.userInfo.password2 })
      //   this.$store.dispatch('user/getUserInfo') // 这种方案ok
      this['user/getUserInfo']
      this.$message.success('更新数据成功')
    }
  }
}
</script>

<style>

</style>
